/*
 * Teknei 2014 - All rights reserved
 */
package com.teknei.acabus.core.controller;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.apache.commons.collections.CollectionUtils;

import com.teknei.acabus.FacadeException;
import com.teknei.acabus.core.common.MessageConstants;
import com.teknei.acabus.core.entities.Role;
import com.teknei.acabus.core.facade.NavigationFacade;

/**
 * Controller for Role elements
 * @author Jorge Amaro
 * @since 1.0
 * @version 1.0
 *
 */
@ViewScoped
@ManagedBean(name = "role_controller")
public class RoleController extends GenericController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@EJB
	private NavigationFacade facadeNavigation;
	private List<Role> itemsList;
	private Role newItem;
	private Role currentItem;
	private String input_new_roleName;
	private String input_edit_roleName;

	/**
	 * Postconstruct method. Initialize required values
	 */
	@PostConstruct
	private void postConstruct() {
		initItemList();
	}

	/**
	 * Find all Roles
	 * @return
	 */
	public List<Role> findAll() {
		itemsList = null;
		initItemList();
		return itemsList;
	}

	/**
	 * Initialize items list and validates them
	 */
	private void initItemList() {
		if (CollectionUtils.isEmpty(itemsList)) {
			try {
				itemsList = facadeNavigation.findRoles();
			} catch (FacadeException e) {
				super.addErrorMessage(MessageConstants.ACTION_ERROR_INIT_LIST);
			}
		}
	}

	/**
	 * Refresh lists
	 */
	private void refreshItemsList() {
		itemsList = null;
		initItemList();
		newItem = null;
		currentItem = null;
	}

	/**
	 * Refresh inputs
	 */
	private void refreshInputs() {
		input_new_roleName = "";
		input_edit_roleName = "";
	}

	/**
	 * Saves a record and update the lists
	 */
	public void onSaveRole() {
		newItem = new Role();
		newItem.setRoleName(input_new_roleName);
		try {
			int pk = facadeNavigation.saveRole(newItem);
			newItem.setIdRole(pk);
			itemsList.add(newItem);
			newItem = null;
			refreshInputs();
			super.addInfoMessage(MessageConstants.ACTION_SAVE_ITEM_SUCCESS);
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_SAVE_ITEM_ERROR);
		}
	}

	/**
	 * Cancels the action
	 */
	public void onSaveCancelRole() {
		refreshInputs();
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}

	/**
	 * Updates the Role
	 */
	public void onEditRole() {
		if(input_edit_roleName != null &&  input_edit_roleName.trim().length() > 1){
			currentItem.setRoleName(input_edit_roleName);
		}else{
			super.addWarningMessage(MessageConstants.WARNING_EMPTY_UPDATE_FIELDS);
			return;
		}
		try {
			facadeNavigation.updateRole(currentItem);
			refreshItemsList();
			super.addInfoMessage(MessageConstants.ACTION_UPDATE_SUCCESSFUL);
			currentItem = null;
		} catch (FacadeException e) {
			super.addErrorMessage(MessageConstants.ACTION_UPDATE_FAILURE);
		}
	}

	/**
	 * Cancels the action
	 */
	public void onEditCancelRole() {
		refreshInputs();
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}

	/**
	 * Deletes a role
	 */
	public void onDeleteRole() {
		try {
			facadeNavigation.deleteRole(currentItem);
			refreshItemsList();
			super.addInfoMessage(MessageConstants.ACTION_DELETE_ITEM_SUCCESS,
					MessageConstants.MESSAGE_DELETE_ERROR_HINT);
		} catch (FacadeException e) {
			refreshItemsList();
			super.addErrorMessage(MessageConstants.ACTION_DELETE_ITEM_ERROR,
					MessageConstants.MESSAGE_DELETE_ERROR_HINT);
		}
	}

	/**
	 * Cancels the action
	 */
	public void onDeleteCancelRole() {
		super.addInfoMessage(MessageConstants.ACTION_CANCELLED);
	}

	/**
	 * @return the facadeNavigation
	 */
	public NavigationFacade getFacadeNavigation() {
		return facadeNavigation;
	}

	/**
	 * @param facadeNavigation
	 *            the facadeNavigation to set
	 */
	public void setFacadeNavigation(NavigationFacade facadeNavigation) {
		this.facadeNavigation = facadeNavigation;
	}

	/**
	 * @return the itemList
	 */
	public List<Role> getItemsList() {
		initItemList();
		return itemsList;
	}

	/**
	 * @param itemList
	 *            the itemList to set
	 */
	public void setItemsList(List<Role> itemList) {
		this.itemsList = itemList;
	}

	/**
	 * @return the input_new_roleName
	 */
	public String getInput_new_roleName() {
		return input_new_roleName;
	}

	/**
	 * @param input_new_roleName
	 *            the input_new_roleName to set
	 */
	public void setInput_new_roleName(String input_new_roleName) {
		this.input_new_roleName = input_new_roleName;
	}

	/**
	 * @return the input_edit_roleName
	 */
	public String getInput_edit_roleName() {
		return input_edit_roleName;
	}

	/**
	 * @param input_edit_roleName
	 *            the input_edit_roleName to set
	 */
	public void setInput_edit_roleName(String input_edit_roleName) {
		this.input_edit_roleName = input_edit_roleName;
	}

	/**
	 * @return the newItem
	 */
	public Role getNewItem() {
		return newItem;
	}

	/**
	 * @param newItem
	 *            the newItem to set
	 */
	public void setNewItem(Role newItem) {
		this.newItem = newItem;
	}

	/**
	 * @return the currentItem
	 */
	public Role getCurrentItem() {
		return currentItem;
	}

	/**
	 * @param currentItem
	 *            the currentItem to set
	 */
	public void setCurrentItem(Role currentItem) {
		this.currentItem = currentItem;
	}

}
